﻿using AKBGame.Object;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AKBGame.Util
{
    public class ListViewLoveComparer : IComparer
    {
        private int col;
        private SortOrder order;
        private List<MemberIDMapping> memberIDMappingList;

        public ListViewLoveComparer()
        {
            col = 0;
            order = SortOrder.Ascending;
            memberIDMappingList = new List<MemberIDMapping>();
        }

        public ListViewLoveComparer(int column, SortOrder order, List<MemberIDMapping> memberIDMappingList)
        {
            col = column;
            this.order = order;
            this.memberIDMappingList = memberIDMappingList;
        }

        public int Compare(object x, object y) 
        {
            int returnVal = -1;
            int a, b;

            //name sort
            if (col == 0)
            {
                CultureInfo ci = new CultureInfo("ja-JP");

                string c = memberIDMappingList.Single(o => o.Name == ((ListViewItem)x).SubItems[col].Text).NameHiragana;
                string d = memberIDMappingList.Single(o => o.Name == ((ListViewItem)y).SubItems[col].Text).NameHiragana;
                returnVal = string.Compare(c, d, ci, CompareOptions.None);
            }

            //int sort
            else if (int.TryParse(((ListViewItem)x).SubItems[col].Text, out a) && int.TryParse(((ListViewItem)y).SubItems[col].Text, out b))
            {
                returnVal = a == b ? 0 : a > b ? -1 : 1;
            }

            else //string sort
            {
                string c = ((ListViewItem)x).SubItems[col].Text;
                string d = ((ListViewItem)y).SubItems[col].Text;

                returnVal = string.Compare(c, d);
            }

            if (order == SortOrder.Descending)
                returnVal *= -1;

            return returnVal;
        }
    }
}
